#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_uart.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "uart.h"
#include "..\osram96x16x1.h"
#include "uart_clock.h"
#include "timers.h"
#include "stdlib.h"
#include "uart_menu.h"
#include "adc.h"

#define PUSH_BUTTON		GPIO_PIN_4		// Button pin

// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif


// The UART interrupt handler.
//
//*****************************************************************************
void
UARTIntHandler(void)
{
    unsigned long ulStatus;

    //
    // Get the interrrupt status.
    //
    ulStatus = UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    UARTIntClear(UART0_BASE, ulStatus);

    //
    // Loop while there are characters in the receive FIFO.
    //
    while(UARTCharsAvail(UART0_BASE))
    {
        //
        // Read the next character from the UART and write it back to the UART.
        //
        UARTCharPutNonBlocking(UART0_BASE, UARTCharGetNonBlocking(UART0_BASE));
    }
}


// Send a string to the UART.
//
//*****************************************************************************

void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
		while(! UARTSpaceAvail(UART0_BASE) )
		{
		}
    //
    // Write the next character to the UART.
    //
    UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    
	}
}


// Check for correct input and respond through UART
//
void EnterTime(void) 
{    

	clock tmpStruct;
	char buff[3] = {1,1,0};
	char ch[2] = {1};
	unsigned int i = 0;
	
	beg:
	while( 1 ) {
		
		i = 0;
		UARTSend((unsigned char *)"\n\rEnter hours: ", 15); 
		
		while(1) {
			
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		
		 }
		if( CheckInput(buff,1) ) {
			tmpStruct.hour = atoi(buff);

			break;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);
	}	
	
	while(1) {		
		
		i = 0;	
		UARTSend((unsigned char *)"\n\rEnter minutes: ",17 ) ;
		
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		}
		if(CheckInput(buff,2))
			{ 
				tmpStruct.min = atoi(buff);
		  		break;
			}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);    
	}	  
	  
	while(1) {		 
		
		i = 0;
		UARTSend((unsigned char *)"\n\rEnter seconds: ",17 ) ;
		  
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		}
		if(CheckInput(buff,2))
		{
			tmpStruct.sec = atoi(buff); 
		 	break;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);    
	}	  	  
	 
	UARTSend((unsigned char *)"\n\rCorrect!", 10);
	
	
	while(1) {		 
		
		i = 0;
		UARTSend((unsigned char *)"\n\rSave and Exit: Y/N", 22);
		  
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					ch[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, ch[i] ) ;
					i++;
			}
			if(ch[i] == '\0') break;
		}
		
		if(ch[0] == 'y' || ch[0] == 'Y')
		{
			displayTime(tmpStruct, 1); 
		 	break;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);
		  goto beg;    
	}
	
}	

int CheckInput(char buff[3], unsigned int choice) 
{
	switch (choice) 
	{
		case 1:	 if(( (buff[0] == '0' || buff[0] == '1') && (buff[1] >= '0' && buff[1] <= '9') )
		 			|| ( ( buff[0] == '2') && (buff[1] >= '0' && buff[1] <= '3') ))	return 1; 
					break; 
		 		
		case 2:	 if( (buff[0] >= '0' && buff[0] <= '5') && (buff[1] >= '0' && buff[1] <= '9') )		
				    return 1;	
					 break; 	 			
	}
	return 0;
}

void GetDataFromACP()
{
	int f1;
  	unsigned long value[8];
  	clock tmpStruct;
  	static int perTemp=0, status = 0;
  

	while (1)
	{
		if (button_flag)
		{
			button_flag = 0;
		 	break;
		}  
		else
		{
			ADCProcessorTrigger(ADC_BASE, 0);
 	 		while(!ADCIntStatus(ADC_BASE, 0, false));
 			ADCSequenceDataGet(ADC_BASE, 0, value);

	  		if(perTemp<(value[0]-22) || perTemp>(value[0]+22))
	  		{
	    		perTemp=value[0];
	    		tmpStruct.hour   = (int)((value[0]*25)/1023);
			
				if(tmpStruct.hour == 24 || tmpStruct.hour == 25)
				tmpStruct.hour = 23;

				_displayTime(tmpStruct, 1);

	  		}
		}
	}

	while (1)
	{
		if (button_flag)
		{
			button_flag = 0;
		 	break;
		}  
		else
		{
			ADCProcessorTrigger(ADC_BASE, 0);
 	 		while(!ADCIntStatus(ADC_BASE, 0, false));
 			ADCSequenceDataGet(ADC_BASE, 0, value);

	  		switch (status)
	  		{
	  		case 0:	 if(perTemp<(value[0]-9) || perTemp>(value[0]+9))
			{
			  	perTemp=value[0];
				tmpStruct.min   = (int)((value[0]*62)/1023);
				if(tmpStruct.min == 60 || tmpStruct.min == 61 || tmpStruct.min == 62 )
					tmpStruct.min = 59;
				_displayTime(tmpStruct,1);
				status = 1;
			} break;

			case 1:	  if(perTemp<(value[0]-15) || perTemp>(value[0]+15))
					status = 0; break;
	  		}
		}
	}
			
	while (1)
	{
		
	    if (button_flag)
		{
			button_flag = 0;
		 	break;
		}  
		else
		{
			ADCProcessorTrigger(ADC_BASE, 0);
 	 		while(!ADCIntStatus(ADC_BASE, 0, false));
 			ADCSequenceDataGet(ADC_BASE, 0, value);

		 	switch (status)
	  		{
	  		case 0:	 if(perTemp<(value[0]-9) || perTemp>(value[0]+9))
			{
			   	perTemp=value[0];	    	
				tmpStruct.sec   = (int)((value[0]*62)/1023);
				if(tmpStruct.sec == 60 || tmpStruct.sec == 61 || tmpStruct.sec == 62 )
				tmpStruct.sec = 59;
				_displayTime(tmpStruct,1);
				status = 1;
			}break;
			case 1:	  if(perTemp<(value[0]-15) || perTemp>(value[0]+15))
			status = 0; break;
			}	  
		}
	}

	displayTime(tmpStruct, 1);
}



